/**
 * 
 */
package com.scpii.universal.service.mobile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.scpii.universal.hibernate.entity.AppDetailPageTab;
import com.scpii.universal.hibernate.entity.AttributeDto;
import com.scpii.universal.hibernate.entity.AttributeListTab;
import com.scpii.universal.hibernate.entity.AttributeTab;
import com.scpii.universal.hibernate.entity.AttributeValueTab;
import com.scpii.universal.hibernate.entity.CatelogTab;
import com.scpii.universal.hibernate.entity.ContentImgTab;
import com.scpii.universal.hibernate.entity.ContentTab;
import com.scpii.universal.hibernate.entity.EnterpShopTab;
import com.scpii.universal.hibernate.entity.SearchField;
import com.scpii.universal.pb.PbUniversal.PBContentSearchCS;
import com.scpii.universal.pb.PbUniversal.PBContentSearchFieldCS;
import com.scpii.universal.pb.PbUniversal.PBContentSearchFieldSC;
import com.scpii.universal.pb.PbUniversal.PBContentSearchSC;
import com.scpii.universal.pb.PbUniversal.PBHouseContentDetailCS;
import com.scpii.universal.pb.PbUniversal.PBHouseContentDetailSC;
import com.scpii.universal.pb.PbUniversal.PBRESULT;
import com.scpii.universal.pb.PbUniversal.PbAppContentCS;
import com.scpii.universal.pb.PbUniversal.PbAppContentListCS;
import com.scpii.universal.pb.PbUniversal.PbAppContentListSC;
import com.scpii.universal.pb.PbUniversal.PbAppContentSC;
import com.scpii.universal.pb.PbUniversal.PbContentCatelogListCS;
import com.scpii.universal.pb.PbUniversal.PbContentCatelogListSC;
import com.scpii.universal.pb.PbUniversal.PbContentImgListCS;
import com.scpii.universal.pb.PbUniversal.PbContentImgListSC;
import com.scpii.universal.pb.PbUniversalBasic.PbAppContent;
import com.scpii.universal.pb.PbUniversalBasic.PbAppContentList;
import com.scpii.universal.pb.PbUniversalBasic.PbAppPageContent;
import com.scpii.universal.pb.PbUniversalBasic.PbAppPageContentList;
import com.scpii.universal.pb.PbUniversalBasic.PbCatelogAttribute;
import com.scpii.universal.pb.PbUniversalBasic.PbCatelogAttributeList;
import com.scpii.universal.pb.PbUniversalBasic.PbContentAttributeValue;
import com.scpii.universal.pb.PbUniversalBasic.PbContentAttributeValueList;
import com.scpii.universal.pb.PbUniversalBasic.PbContentCatelog;
import com.scpii.universal.pb.PbUniversalBasic.PbContentCatelogList;
import com.scpii.universal.pb.PbUniversalBasic.PbContentImg;
import com.scpii.universal.pb.PbUniversalBasic.PbContentImgList;
import com.scpii.universal.pb.PbUniversalBasic.PbEnterpShop;
import com.scpii.universal.pb.PbUniversalBasic.PbEnterpShopList;
import com.scpii.universal.pb.PbUniversalBasic.PbForSearchField;
import com.scpii.universal.pb.PbUniversalBasic.PbForSearchFieldList;
import com.scpii.universal.pb.PbUniversalBasic.PbSearchField;
import com.scpii.universal.pb.PbUniversalBasic.PbSearchFieldBasic;
import com.scpii.universal.pb.PbUniversalBasic.PbSearchFieldList;
import com.scpii.universal.pb.PbUniversalBasic.PbSelectOption;
import com.scpii.universal.pb.PbUniversalBasic.PbSelectOptionList;
import com.scpii.universal.service.AppDetailPageTabService;
import com.scpii.universal.service.AttributeListTabService;
import com.scpii.universal.service.AttributeTabService;
import com.scpii.universal.service.AttributeValueTabService;
import com.scpii.universal.service.CatelogTabService;
import com.scpii.universal.service.ContentImgTabService;
import com.scpii.universal.service.ContentTabService;
import com.scpii.universal.service.EnterpShopTabService;
import com.scpii.universal.service.PageModuleConTabService;
import com.scpii.universal.util.Const;
import com.scpii.universal.util.XmlUtil;

/**
 * UniversalApi
 * @author 甘建新
 * 2012-7-11下午11:01:50
 */
public class MobileContentService {
//	private ContentTabService contentTabService = new ContentTabService();
//	private AttributeTabService attributeTabService = new AttributeTabService();
//	private AttributeValueTabService attributeValueTabService = new AttributeValueTabService();
//	private ContentImgTabService contentImgTabService = new ContentImgTabService();
//	private CatelogTabService catelogTabService = new CatelogTabService();
//	private AppDetailPageTabService appDetailPageTabService = new AppDetailPageTabService();
	
	private ContentTabService contentTabService ;
	private AttributeTabService attributeTabService;
	private AttributeValueTabService attributeValueTabService;
	private ContentImgTabService contentImgTabService;
	private CatelogTabService catelogTabService;
	private EnterpShopTabService enterpShopTabService;
	private AppDetailPageTabService appDetailPageTabService;
	private AttributeListTabService attributeListTabService;
	private PageModuleConTabService pageModuleConTabService;
	
	private Logger log = Logger.getLogger(MobileContentService.class);
	private String LOG_METHOD_NAME="";
	/**
	 * 获得详细内容
	 * GET_CONTENT
	 * @param appContentCS
	 * @return
	 * @author 甘建新
	 * 2012-7-11下午11:18:21
	 */
	public byte[] getContent(byte[] bytes){
		LOG_METHOD_NAME="getContent";
		PbAppContentSC.Builder pbAppContentSCBuilder = PbAppContentSC.newBuilder();
		PbAppContent.Builder pbAppContentBuilder = PbAppContent.newBuilder();
		if (bytes!=null && bytes.length>0) {
			try {
				PbAppContentCS pbAppContentCS = PbAppContentCS.parseFrom(bytes);
				int appId = pbAppContentCS.getIntAppId();
				int contentId = pbAppContentCS.getIntContentId();
				
				log.info(LOG_METHOD_NAME+LOG_METHOD_NAME+"【appId:"+appId+",contentId:"+contentId+"】");
				
				//获取详细内容数据
				ContentTab contentTab = contentTabService.get(contentId);
				if (contentTab!=null) {
					pbAppContentBuilder.setIntAppId(contentTab.getIntAppId());
					pbAppContentBuilder.setIntCateId(contentTab.getIntCateId());
					pbAppContentBuilder.setIntContentId(contentTab.getIntContentId());
					pbAppContentBuilder.setIntEnterpId(contentTab.getIntEnterpId());
					pbAppContentBuilder.setIntViewCount(contentTab.getIntViewCount());
					
					pbAppContentBuilder.setVarContentDescrip(contentTab.getVarContentDescript());
					pbAppContentBuilder.setVarContentImgUrl(contentTab.getVarContentImgUrl());
					pbAppContentBuilder.setVarContentName(contentTab.getVarContentName());
					pbAppContentBuilder.setVarResourceId(contentTab.getIntResourceId().toString());
					//获取要显示的模块
					CatelogTab catelogTab = catelogTabService.getSuperCatelogByCateId(contentTab.getIntCateId());
					int superCatelogId =0;
					if (catelogTab!=null) {
						superCatelogId = catelogTab.getIntCateId();
					}
					String varDisplayModules = pageModuleConTabService.getDisplayModules(superCatelogId, contentTab.getIntAppId());
					pbAppContentBuilder.setVarDisplayModules(varDisplayModules);
					
					if (varDisplayModules.contains("4")) {
						//设置内容中的属性值对
//						List<AttributeValueTab> attrValueList = attributeValueTabService.getAttributeValueListByContentId(contentTab.getIntContentId());
						List<AttributeDto> attrValueList = attributeValueTabService.getAttrValueAndAttrInfo(contentTab.getIntContentId());
						PbContentAttributeValueList.Builder pbContentAttrValueListBuilder = PbContentAttributeValueList.newBuilder();
						if (attrValueList!=null && attrValueList.size()>0) {
							//转换数据
							AttributeValueTab attrValueTab = null;
							for (AttributeDto attributeDto : attrValueList) {
								attrValueTab = attributeDto.getAttributeTab();
								PbContentAttributeValue.Builder pbContentAttrValueBuilder = PbContentAttributeValue.newBuilder();
								pbContentAttrValueBuilder.setIntAttriId(attrValueTab.getIntAttriId());
								pbContentAttrValueBuilder.setIntAttriValueId(attrValueTab.getIntAttriValueId());
								pbContentAttrValueBuilder.setIntContentId(attrValueTab.getIntContentId());
								pbContentAttrValueBuilder.setVarAttriValue(attrValueTab.getVarAttriValue());
								pbContentAttrValueBuilder.setVarAttriName(attributeTabService.get(attrValueTab.getIntAttriId()).getVarAttriName());
								pbContentAttrValueBuilder.setIntAttrType(attributeDto.getIntType());
								
								pbContentAttrValueListBuilder.addPbContentAttributeValueList(pbContentAttrValueBuilder);
							}
						}
						pbAppContentBuilder.setPbContentAttributeValueList(pbContentAttrValueListBuilder);
					}
					
					//设置内容中包含图片列表
					//取得图片列表包括音频视频
					List<ContentImgTab> contentImgList = contentImgTabService.getResourceByContentId(contentTab.getIntContentId(), Const.CONTENT_IMG_TYPE_PHOTO);
					//初始化PbContentImgList对象
					PbContentImgList.Builder pbImgListBuilder = PbContentImgList.newBuilder();
					PbContentImgList.Builder pbViedoListBuilder = PbContentImgList.newBuilder();
					PbContentImgList.Builder pbRadioListBuilder = PbContentImgList.newBuilder();
					if (contentImgList!=null && contentImgList.size()>0) {
						for (ContentImgTab contentImgTab : contentImgList) {
							PbContentImg.Builder pbContentImgBuilder = PbContentImg.newBuilder();
							pbContentImgBuilder.setIntContentId(contentImgTab.getIntContentId());
							pbContentImgBuilder.setIntImgId(contentImgTab.getIntImgId());
							pbContentImgBuilder.setVarImgName(contentImgTab.getVarImgName());
							pbContentImgBuilder.setVarImgUrl(contentImgTab.getVarImgUrl());
							pbContentImgBuilder.setVarImgDescribe(contentImgTab.getVarImgDescrip()==null?"":contentImgTab.getVarImgDescrip());
							if(contentImgTab.getIntType()==Const.CONTENT_IMG_TYPE_DETAIL){
								pbImgListBuilder.addPbContentImgList(pbContentImgBuilder);
							} else if (contentImgTab.getIntType()==Const.CONTENT_IMG_TYPE_RADIO){
								pbRadioListBuilder.addPbContentImgList(pbContentImgBuilder);
							} else if (contentImgTab.getIntType()==Const.CONTENT_IMG_TYPE_VIDEO) {
								pbViedoListBuilder.addPbContentImgList(pbContentImgBuilder);
							}
						}
					}
					pbAppContentBuilder.setPbContentImgList(pbImgListBuilder);
					pbAppContentBuilder.setPbContentVedio(pbViedoListBuilder);
					pbAppContentBuilder.setPbContentRadio(pbRadioListBuilder);
					
					//将数据设置到返回数据中
					pbAppContentSCBuilder.setPbAppContent(pbAppContentBuilder);
					pbAppContentSCBuilder.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {//设置数据获取异常状态
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbAppContentSCBuilder.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {//设置数据获取异常的状态
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbAppContentSCBuilder.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {//设置数据获取失败的其他异常情况状态
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbAppContentSCBuilder.setExeResult(PBRESULT.FAILED);
		}
		return pbAppContentSCBuilder.build().toByteArray();
	}
	
	/**
	 * 获取内容列表
	 * GET_CONTENTLIST
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-7-17上午01:32:25
	 */
	public byte[] getContentList(byte[] bytes){
		LOG_METHOD_NAME="getContentList";
		PbAppContentListSC.Builder pbAppContentListSCB = PbAppContentListSC.newBuilder();
		PbAppContentList.Builder pbAppContentListB = PbAppContentList.newBuilder();
		if (bytes !=null) {
			try {
				PbAppContentListCS pbAppContentListCS = PbAppContentListCS.parseFrom(bytes);
				int appId = pbAppContentListCS.getIntAppId();
				int catelogId = pbAppContentListCS.getIntCatelogId();
				
				log.info(LOG_METHOD_NAME+Const.LOG_METHOD_PARAM+"【appId:"+appId+",catelogId:"+catelogId+"】");
				
				//获取数据
				List<ContentTab> contentList = contentTabService.getContentListByCatelogId(catelogId,appId);
				if (contentList!=null && contentList.size()>0) {
					for (ContentTab contentTab : contentList) {
						PbAppContent.Builder pbAppContentBuilder = PbAppContent.newBuilder();
						pbAppContentBuilder.setIntAppId(contentTab.getIntAppId());
						pbAppContentBuilder.setIntCateId(contentTab.getIntCateId());
						pbAppContentBuilder.setIntContentId(contentTab.getIntContentId());
						pbAppContentBuilder.setIntEnterpId(contentTab.getIntEnterpId());
						pbAppContentBuilder.setIntViewCount(contentTab.getIntViewCount());
						
						pbAppContentBuilder.setVarContentDescrip(contentTab.getVarContentDescript());
						pbAppContentBuilder.setVarContentImgUrl(contentTab.getVarContentImgUrl());
						pbAppContentBuilder.setVarContentName(contentTab.getVarContentName());
						pbAppContentBuilder.setVarResourceId(contentTab.getIntResourceId().toString());
						
						//设置内容中的属性值对
						List<AttributeValueTab> attrValueList = attributeValueTabService.getAttributeValueListByContentId(contentTab.getIntContentId());
						PbContentAttributeValueList.Builder pbContentAttrValueListBuilder = PbContentAttributeValueList.newBuilder();
						if (attrValueList!=null && attrValueList.size()>0) {
							//转换数据
							for (AttributeValueTab attrValueTab : attrValueList) {
								PbContentAttributeValue.Builder pbContentAttrValueBuilder = PbContentAttributeValue.newBuilder();
								pbContentAttrValueBuilder.setIntAttriId(attrValueTab.getIntAttriId());
								pbContentAttrValueBuilder.setIntAttriValueId(attrValueTab.getIntAttriValueId());
								pbContentAttrValueBuilder.setIntContentId(attrValueTab.getIntContentId());
								pbContentAttrValueBuilder.setVarAttriValue(attrValueTab.getVarAttriValue());
								String attrName="";
								AttributeTab attributeTab = attributeTabService.get(attrValueTab.getIntAttriId());
								if (attributeTab!=null) {
									attrName = attributeTab.getVarAttriName();
								}
								pbContentAttrValueBuilder.setVarAttriName(attrName!=null?attrName:"");
								
								pbContentAttrValueListBuilder.addPbContentAttributeValueList(pbContentAttrValueBuilder);
							}
						}
						pbAppContentBuilder.setPbContentAttributeValueList(pbContentAttrValueListBuilder);
						
						//设置内容中包含图片列表
						//取得图片列表
						List<ContentImgTab> contentImgList = contentImgTabService.getImgListByContentId(contentTab.getIntContentId(), Const.CONTENT_IMG_TYPE_DETAIL);
						//初始化PbContentImgList对象
						PbContentImgList.Builder pbImgListBuilder = PbContentImgList.newBuilder();
						if (contentImgList!=null && contentImgList.size()>0) {
							for (ContentImgTab contentImgTab : contentImgList) {
								PbContentImg.Builder pbContentImgBuilder = PbContentImg.newBuilder();
								pbContentImgBuilder.setIntContentId(contentImgTab.getIntContentId());
								pbContentImgBuilder.setIntImgId(contentImgTab.getIntImgId());
								pbContentImgBuilder.setVarImgName(contentImgTab.getVarImgName());
								pbContentImgBuilder.setVarImgUrl(contentImgTab.getVarImgUrl());
								
								pbImgListBuilder.addPbContentImgList(pbContentImgBuilder);
							}
						}
						pbAppContentBuilder.setPbContentImgList(pbImgListBuilder);
						
						//将pbAppContentBuilder设置到List当中
						pbAppContentListB.addPbAppContentList(pbAppContentBuilder);
					}
					//将数据设置到返回数据中
					pbAppContentListSCB.setPbAppContentList(pbAppContentListB);
					pbAppContentListSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else{
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbAppContentListSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {//设置数据获取异常状态
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbAppContentListSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {//设置数据获取其他情况导致无法获得数据
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbAppContentListSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbAppContentListSCB.build().toByteArray();
	}
	
	/**
	 * 获得内容分类列表
	 * GET_CONTENT_CATELOGLIST
	 * @param contentCatelogListCS
	 * @return
	 * @author 甘建新
	 * 2012-7-11下午11:20:13
	 */
	public byte[] getContentCatelogList(byte[] bytes){
		LOG_METHOD_NAME="getContentCatelogList";
		
		PbContentCatelogListSC.Builder pbContCateListSCB = PbContentCatelogListSC.newBuilder();
		PbContentCatelogList.Builder pbContCateList = PbContentCatelogList.newBuilder();
		if (bytes!=null) {
			try {
				//转换参数
				PbContentCatelogListCS pbContCateListCS = PbContentCatelogListCS.parseFrom(bytes);
				//取得参数
				int appId = pbContCateListCS.getIntAppId();
				int catelogId = pbContCateListCS.getIntCateId();
				log.info(LOG_METHOD_NAME+Const.LOG_METHOD_PARAM+"【appId:"+appId+",catelogId:"+catelogId+"】");
				//查询满足条件的数据
				List<CatelogTab> cateList = catelogTabService.getCatelogListByParentCatelogId(appId,catelogId);
				if (cateList!=null && cateList.size()>0) {
					for (CatelogTab cate : cateList) {
						PbContentCatelog.Builder pbCateB = PbContentCatelog.newBuilder();
						pbCateB.setIntAppId(cate.getIntAppId());
						pbCateB.setIntCateId(cate.getIntCateId());
						pbCateB.setIntEnterpId(cate.getIntEnterpId());
						pbCateB.setIntParentCateId(cate.getIntParentCateId());
						
						pbCateB.setVarCateImgUrl(cate.getVarCateImgUrl());
						pbCateB.setVarCateName(cate.getVarCateName());
						pbCateB.setIntCateType(cate.getIntType());
						
						//获得分类包含的属性列表
						List<AttributeTab> attriList = attributeTabService.getAttributeListByCatelogId(cate.getIntCateId());
						PbCatelogAttributeList.Builder pbCateAttrListB = PbCatelogAttributeList.newBuilder();
						if (attriList!=null && attriList.size()>0) {
							for (AttributeTab attr : attriList) {
								PbCatelogAttribute.Builder pbCateAttrB = PbCatelogAttribute.newBuilder();
								pbCateAttrB.setIntAttrId(attr.getIntAttriId());
								pbCateAttrB.setIntCateId(attr.getIntCateId());
								pbCateAttrB.setVarAttriName(attr.getVarAttriName());
								
								pbCateAttrListB.addPbCatelogAttribute(pbCateAttrB);
							}
						}
						pbCateB.setPbCatelogAttributeList(pbCateAttrListB);
						//添加到List中
						pbContCateList.addPbContentCatelog(pbCateB);
					}
					//添加到返回数据中
					pbContCateListSCB.setPbContentCatelogList(pbContCateList);
					pbContCateListSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {//获取数据失败状态
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbContCateListSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {//获取数据异常 状态
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbContCateListSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {//获取数据其他情况引起的异常状态
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbContCateListSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbContCateListSCB.build().toByteArray();
	}
	
	/**
	 * 获得详细内容的图片
	 * GET_CONTENT_IMGLIST
	 * @param contentImgListCS
	 * @return
	 * @author 甘建新
	 * 2012-7-11下午11:22:48
	 */
	public byte[] getContentImgList(byte[] bytes){
		LOG_METHOD_NAME="getContentImgList";
		
		PbContentImgListSC.Builder pbContImgListSCB = PbContentImgListSC.newBuilder();
		PbContentImgList.Builder pbContImgListB = PbContentImgList.newBuilder();
		if (bytes!=null) {
			try {
				PbContentImgListCS pbContImgListCS = PbContentImgListCS.parseFrom(bytes);
				int appId = pbContImgListCS.getIntAppId();
				int contentId = pbContImgListCS.getIntContentId();
				
				log.info(LOG_METHOD_NAME+Const.LOG_METHOD_PARAM+"【appId:"+appId+",contentId:"+appId+"】");
				
				//设置内容中包含图片列表
				//取得图片列表
				List<ContentImgTab> contentImgList = contentImgTabService.getImgListByContentId(contentId, Const.CONTENT_IMG_TYPE_DETAIL);
				//初始化PbContentImgList对象
				if (contentImgList!=null && contentImgList.size()>0) {
					for (ContentImgTab contentImgTab : contentImgList) {
						PbContentImg.Builder pbContentImgBuilder = PbContentImg.newBuilder();
						pbContentImgBuilder.setIntContentId(contentImgTab.getIntContentId());
						pbContentImgBuilder.setIntImgId(contentImgTab.getIntImgId());
						pbContentImgBuilder.setVarImgName(contentImgTab.getVarImgName());
						pbContentImgBuilder.setVarImgUrl(contentImgTab.getVarImgUrl());
						
						pbContImgListB.addPbContentImgList(pbContentImgBuilder);
					}
					//设置到返回数据中
					pbContImgListSCB.setPbContentImgList(pbContImgListB);
					pbContImgListSCB.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pbContImgListSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pbContImgListSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pbContImgListSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbContImgListSCB.build().toByteArray();
	}

	
	
	/**
	 * 获得详细页面访问地址 
	 * @param bytes
	 * @return
	 * @author 甘建新
	 * 2012-7-24上午11:14:09
	 */
	/**
	public byte[] getContentDetailPage(byte[] bytes){
		PbGetContentDetailPageUrlSC.Builder pbGetConDetilPageUrlSCB = PbGetContentDetailPageUrlSC.newBuilder();
		PbContentDetailPageInfo.Builder pbContDetilPageInfoB = PbContentDetailPageInfo.newBuilder();
		if (bytes!=null) {
			try {
				PbGetContentDetailPageUrlCS pbDetailPageCS = PbGetContentDetailPageUrlCS.parseFrom(bytes);
				int appId = pbDetailPageCS.getIntAppId();
				int contentId = pbDetailPageCS.getIntContentId();
				
				//获得内容信息
				ContentTab contentTab = contentTabService.get(contentId);
				int catelogId = contentTab.getIntCateId();
				//获取顶级分类信息
				CatelogTab catelogTab = catelogTabService.getSuperCatelogByCateId(catelogId);
				int superCatelogId = catelogTab.getIntCateId();
				//查询详情页面表信息
				AppDetailPageTab appDetailPageTab = appDetailPageTabService.getAppDetailInfo(appId, superCatelogId);
				if (appDetailPageTab!=null) {
					pbContDetilPageInfoB.setIntAppId(appId);
					pbContDetilPageInfoB.setIntPageDetailId(appDetailPageTab.getIntPageDetailId());
					pbContDetilPageInfoB.setVarDetailPageName(appDetailPageTab.getVarPageDetailName());
					pbContDetilPageInfoB.setVarDetailPageType(appDetailPageTab.getIntPageDetailType());
					pbContDetilPageInfoB.setVarDetailPageURL(appDetailPageTab.getVarPageUrl()+contentId);
					
					//设置携带的部分内容信息
					PbAppContent.Builder pbAppConB = PbAppContent.newBuilder();
					pbAppConB.setIntAppId(appId);
					pbAppConB.setIntCateId(contentTab.getIntCateId());
					pbAppConB.setIntContentId(contentTab.getIntContentId());
					pbAppConB.setIntEnterpId(contentTab.getIntEnterpId());
					pbAppConB.setIntViewCount(contentTab.getIntViewCount());
					pbAppConB.setVarContentDescrip(contentTab.getVarContentDescript());
					pbAppConB.setVarContentImgUrl(contentTab.getVarContentImgUrl());
					pbAppConB.setVarContentName(contentTab.getVarContentName());
					
					pbContDetilPageInfoB.setPbAppContent(pbAppConB);
					
					pbGetConDetilPageUrlSCB.setPbContentDetailPageInfo(pbContDetilPageInfoB);
					pbGetConDetilPageUrlSCB.setExeResult(PBRESULT.SUCCESS);
				}else {
					pbGetConDetilPageUrlSCB.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {
				e.printStackTrace();
				pbGetConDetilPageUrlSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {
			pbGetConDetilPageUrlSCB.setExeResult(PBRESULT.FAILED);
		}
		return pbGetConDetilPageUrlSCB.build().toByteArray();
	}
	**/
	
	/**
	 * 获取房产详情
	 * @author Taylor
	 * 2012-9-16 上午12:15:16
	 * @param bytes
	 * @return
	 * byte[]
	 */
	public byte[] getHouseContentDetail(byte[] bytes){
		LOG_METHOD_NAME="getHouseContentDetail";
		
		PBHouseContentDetailSC.Builder pBHouseContentDetailSCB = PBHouseContentDetailSC.newBuilder();//返回集
		if (bytes != null) {
			try {
				PBHouseContentDetailCS pBHouseContentDetailCS = PBHouseContentDetailCS.parseFrom(bytes);//请求参数
				int contentId = pBHouseContentDetailCS.getIntContentId();
				int appId = pBHouseContentDetailCS.getIntAppId();
				
				log.info(LOG_METHOD_NAME+Const.LOG_METHOD_PARAM+"【contentId:"+contentId+",appId:"+appId+"】");
				
				//取得详情URL
				String detailUrl = "";
				ContentTab curContent = contentTabService.get(contentId);
				CatelogTab catelogTab = catelogTabService.getSuperCatelogByCateId(curContent.getIntCateId());
				int superCatelogId =0;
				if (catelogTab!=null) {
					superCatelogId = catelogTab.getIntCateId();
				}
				AppDetailPageTab appDetailPageTab = appDetailPageTabService.getAppDetailInfo(catelogTab.getIntAppId(), superCatelogId);
				if (appDetailPageTab != null) {
					detailUrl = appDetailPageTab.getVarPageUrl()+contentId;
					pBHouseContentDetailSCB.setVarDetailUrl(detailUrl);
				}
				
				//取得联系我们电话
				if (catelogTab!=null) {
					//取得根分类属性类型为电话的属性
					AttributeTab attr = attributeTabService.getAttributeByType(catelogTab.getIntCateId(), Const.ATTRIBUTE_TYPE_PHONE);
					if (attr != null) {
						//根据属性内容ID取得属性值
						AttributeValueTab attrValue = attributeValueTabService.getAttributeValueByContAttrId(contentId, attr.getIntAttriId());
						if(attrValue != null && !StringUtils.isBlank(attrValue.getVarAttriValue())){
							pBHouseContentDetailSCB.setVarPhoneNum(attrValue.getVarAttriValue());
						}
					}
				}
				
				//取得地图导航
				PbEnterpShopList.Builder pbEnterpShopList = PbEnterpShopList.newBuilder();
				List<EnterpShopTab> enterpShopList = enterpShopTabService.getShopListByAppId(appId, contentId);
				if (enterpShopList != null && enterpShopList.size() > 0) {
					for (EnterpShopTab shop : enterpShopList) {
						PbEnterpShop.Builder pbShopB = PbEnterpShop.newBuilder();
						pbShopB.setIntAppId(shop.getIntAppId());
						pbShopB.setIntEnterpId(shop.getIntEnterpId());
						pbShopB.setIntShopId(shop.getIntShopId());
						pbShopB.setVarShopAddress(shop.getVarShopAddress());
						pbShopB.setVarShopCity(shop.getVarShopCity());
						pbShopB.setVarShopCountry(shop.getVarShopCountry());
						pbShopB.setVarShopLatitude(shop.getVarShopLatitude());
						pbShopB.setVarShopLongitude(shop.getVarShopLongitude());
						pbShopB.setVarShopName(shop.getVarShopName());
						pbShopB.setVarShopPhoneNum(shop.getVarShopPhonenum());
						pbShopB.setVarShopPostNum(shop.getVarShopPostnum());
						pbShopB.setVarShopProvince(shop.getVarShopProvince());
					
						pbEnterpShopList.addPbEnterpShopList(pbShopB);
					}
					pBHouseContentDetailSCB.setPbEnterpShopList(pbEnterpShopList);
				}
				
				//取得相册列表
				PbContentImgList.Builder pbContentImgList = PbContentImgList.newBuilder();
				List<ContentImgTab> contentImgList = contentImgTabService.getImgListByContentId(contentId, Const.CONTENT_IMG_TYPE_PHOTO);
				if (contentImgList != null && contentImgList.size() > 0) {
					for (ContentImgTab contentImgTab : contentImgList) {
						PbContentImg.Builder pbContentImgBuilder = PbContentImg.newBuilder();
						pbContentImgBuilder.setIntContentId(contentImgTab.getIntContentId());
						pbContentImgBuilder.setIntImgId(contentImgTab.getIntImgId());
						pbContentImgBuilder.setVarImgName(contentImgTab.getVarImgName());
						pbContentImgBuilder.setVarImgUrl(contentImgTab.getVarImgUrl());
						pbContentImgList.addPbContentImgList(pbContentImgBuilder);
					}
					pBHouseContentDetailSCB.setPbContentImgList(pbContentImgList);
				}
				pBHouseContentDetailSCB.setExeResult(PBRESULT.SUCCESS);
				log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
			} catch (Exception e) {//设置数据获取异常状态
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pBHouseContentDetailSCB.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {//设置数据获取其他情况导致无法获得数据
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pBHouseContentDetailSCB.setExeResult(PBRESULT.FAILED);
		}
		return pBHouseContentDetailSCB.build().toByteArray();
	}
	
	/**
	 * 获取搜索结果
	 * @author Taylor
	 * 2012-9-18 下午09:38:26
	 * @param bytes
	 * @return
	 * byte[]
	 */
	public byte[] getContentSearchResult(byte[] bytes){
		LOG_METHOD_NAME="getContentSearchResult";
		PBContentSearchSC.Builder pBContentSearchSC = PBContentSearchSC.newBuilder();//返回集
		if (bytes != null) {
			try {
				PBContentSearchCS pBContentSearchCS = PBContentSearchCS.parseFrom(bytes);//请求参数
				int appId = pBContentSearchCS.getIntAppId();
				int pageNo = pBContentSearchCS.getPageNo();//页码
				
				log.info(LOG_METHOD_NAME + Const.LOG_METHOD_PARAM + "【appId:"
						+ appId + ",pageNo:" + pageNo + ",keyWord:"
						+ pBContentSearchCS.getVarKeyWord() + ",sortBy:"
						+ pBContentSearchCS.getSortBy() + ",sortOrder:"
						+ pBContentSearchCS.getSortOrder() + "】");

				List<ContentTab> lstResult = searchContent(pBContentSearchCS);
				if(lstResult!=null && lstResult.size()>0){
					PbAppPageContentList.Builder pbAppPageContentList = PbAppPageContentList.newBuilder();
					for (ContentTab contentTab : lstResult) {
						PbAppPageContent.Builder pbAppPageContB = PbAppPageContent.newBuilder();
						pbAppPageContB.setIntAppId(contentTab.getIntAppId());
						pbAppPageContB.setIntCateId(contentTab.getIntCateId());
						pbAppPageContB.setIntContentId(contentTab.getIntContentId());
						pbAppPageContB.setIntEnterpId(contentTab.getIntEnterpId());
						pbAppPageContB.setIntViewCount(contentTab.getIntViewCount());
						pbAppPageContB.setVarContentDescrip(contentTab.getVarContentDescript());
						pbAppPageContB.setVarContentImgUrl(contentTab.getVarContentImgUrl());
						pbAppPageContB.setVarContentName(contentTab.getVarContentName());
						pbAppPageContB.setVarResourceId(contentTab.getIntResourceId().toString());
						
						//设置内容详情页面访问地址
						String url ="";
						CatelogTab catelogTab = catelogTabService.getSuperCatelogByCateId(contentTab.getIntCateId());
						int superCatelogId =0;
						if (catelogTab!=null) {
							superCatelogId = catelogTab.getIntCateId();
						}
						AppDetailPageTab appDetailPageTab = appDetailPageTabService.getAppDetailInfo(appId, superCatelogId);
						if (appDetailPageTab!=null) {
							url = appDetailPageTab.getVarPageUrl()+contentTab.getIntContentId();
						}
						pbAppPageContB.setVarDetailPageUrl(url);
						//设置页面类型
						pbAppPageContB.setIntDetailPageType(catelogTab.getIntType());
						pbAppPageContB.setDatAddDate(contentTab.getDatAddDatetime());
						
						pbAppPageContentList.addPbAppPageContent(pbAppPageContB);
					}
					pBContentSearchSC.setPbAppPageContentList(pbAppPageContentList);
					pBContentSearchSC.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				}else {
					pBContentSearchSC.setExeResult(PBRESULT.NULL);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
				}
			} catch (Exception e) {//设置数据获取异常状态
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pBContentSearchSC.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {//设置数据获取其他情况导致无法获得数据
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pBContentSearchSC.setExeResult(PBRESULT.FAILED);
		}
		return pBContentSearchSC.build().toByteArray();
	}
	
	/**
	 * 分装内容搜索根据关键字搜索语句
	 * @param keyWord
	 * @param searchSql
	 */
	private void searchByKeyWord(String keyWord,
			StringBuffer searchSql) {
		searchSql.append(" contains(var_content_name,(select p_split_chinese('"+keyWord+"') from dual))>0");
		searchSql.append(" and");
	}
	
	/**
	 * 封装内容搜索共用sql语句（条件，排序）
	 * @param searchSql
	 * @param searchMap
	 * @param pBContentSearchCS
	 */
	private void searchCommonConditionSql(StringBuffer searchSql,
			Map<String, Object> searchMap, PBContentSearchCS pBContentSearchCS) {
		searchSql.append(" int_delete_sign=0 and int_privilege_user=0");
		searchSql.append(" and int_resource_id=0 and int_app_id=:appId");
		searchMap.put("appId", pBContentSearchCS.getIntAppId());
		if(!StringUtils.isBlank(pBContentSearchCS.getSortBy())) {
			searchSql.append(" order by " + pBContentSearchCS.getSortBy());
			if(!StringUtils.isBlank(pBContentSearchCS.getSortOrder())) {
				searchSql.append(" " + pBContentSearchCS.getSortOrder());
			} else {
				searchSql.append(" desc");
			}
			searchSql.append(",int_content_id desc");
		} else {
			searchSql.append(" order by int_content_id desc");//default order
		}
	}
	
	private List<ContentTab> searchContent(PBContentSearchCS pBContentSearchCS) throws Exception {
		StringBuffer searchSql = new StringBuffer("select * from content_tab where");
		Map<String,Object> searchMap = new HashMap<String,Object>();
		if(!StringUtils.isBlank(pBContentSearchCS.getVarKeyWord())) {
			//关键字搜索
			searchByKeyWord(pBContentSearchCS.getVarKeyWord(),searchSql);
		}
		if(pBContentSearchCS.getPbSearchFieldList()!=null){
			//属性搜索
			searchContentByAttrs(pBContentSearchCS, searchSql, searchMap);
		}
		//公共部分
		searchCommonConditionSql(searchSql, searchMap, pBContentSearchCS);
		List<ContentTab> lstResult = contentTabService.getContentByPageNo(pBContentSearchCS.getPageNo(), Const.MOBILE_CLIENT_NO_PERPAGE, searchSql.toString(), searchMap);
		return lstResult;
	}
	
	private void searchContentByAttrs(PBContentSearchCS pBContentSearchCS,
			StringBuffer searchSql,Map<String, Object> searchMap) throws Exception {
		PbSearchFieldList pbSearchFieldList = pBContentSearchCS.getPbSearchFieldList();
		List<PbSearchField> lstSearchFields = pbSearchFieldList.getPbSearchFieldList();
		int count = 0;
		for (PbSearchField pbSearchField : lstSearchFields) {
			PbSearchFieldBasic pbSearchFieldBasic = pbSearchField.getPbSearchFieldBasic();
			int intAttrId = pbSearchFieldBasic.getIntAttrId();//属性ID
			int intAttrType = pbSearchFieldBasic.getIntAttrType();//属性类型
			String fieldValue = pbSearchField.getVarAttrValue().trim();//输入或选择的值
			if (0 == intAttrType && !StringUtils.isBlank(fieldValue)){
				//输入框
				if(count == 0 ) {
					searchSql.append(" int_content_id in(");
				}
				if (count != 0) {
					searchSql.append(" intersect ");
				}
				searchSql.append("(select int_content_id from attribute_value_tab");
				searchSql.append(" where int_delete_sign=0");
				searchSql.append(" and dbms_lob.instr(var_attri_value,:fieldValue"+count+",1,1) > 0");//clob型
				searchSql.append(" and int_attri_id=:intAttrId"+count+")");
				searchMap.put("fieldValue"+count, fieldValue);
				searchMap.put("intAttrId"+count, intAttrId);
				count++;
			}else if (1 == intAttrType && !"-1".equals(fieldValue.trim()) && StringUtils.isNumeric(fieldValue)) {
				//选择项并且不为-1
				if(count == 0 ) {
					searchSql.append(" int_content_id in(");
				}
				if (count != 0) {
					searchSql.append(" intersect ");
				}
				AttributeTab attrTab = attributeTabService.get(intAttrId);
				searchSql.append("(select int_content_id from attribute_value_tab");
				searchSql.append(" where int_delete_sign=0");
				if (attrTab.getIntType() == Const.ATTRIBUTE_TYPE_PRICE) {
					// 价格
					searchSql.append(" and instr(int_attr_list_id,:fieldValue"+count+",1,1) > 0");
					searchMap.put("fieldValue"+count, fieldValue);
					
				} else if (attrTab.getIntType() == Const.ATTRIBUTE_TYPE_HOUSE_TYPE){
					//楼盘形态
					searchSql.append(" and var_attri_value like '%'||(select var_attr_list_value from attribute_list_tab where int_attr_list_id=:attrListId"+count+")||'%'");
					searchMap.put("attrListId"+count, Integer.parseInt(fieldValue));
				} else {
					searchSql.append(" and int_attr_list_id=:fieldValue" + count);
					searchMap.put("fieldValue"+count, Integer.parseInt(fieldValue));
				}
				searchSql.append(" and int_attri_id=:intAttrId"+count+")");
				searchMap.put("intAttrId"+count, intAttrId);
				count++;
			}
		}
		if (count != 0) {
			if (searchSql.lastIndexOf(" intersect ") > 0)
				searchSql.deleteCharAt(searchSql.lastIndexOf(" intersect "));
			searchSql.append(")");
			searchSql.append(" and");
		}
	}
	
	/**
	 * 获取搜索页搜索字段
	 * @author Taylor
	 * 2012-9-18 下午05:53:58
	 * @param bytes
	 * @return
	 * byte[]
	 */
	public byte[] getContentSearchField(byte[] bytes){
		LOG_METHOD_NAME="getContentSearchField";
		
		PBContentSearchFieldSC.Builder pBContentSearchFieldSC = PBContentSearchFieldSC.newBuilder();//返回集
		if (bytes != null) {
			try {
				PBContentSearchFieldCS pBContentSearchFieldCS = PBContentSearchFieldCS.parseFrom(bytes);//请求参数
				int appId = pBContentSearchFieldCS.getIntAppId();
				
				log.info(LOG_METHOD_NAME+Const.LOG_METHOD_PARAM+"【appId:"+appId+"】");
				
				PbForSearchFieldList.Builder pbForSearchFieldList = PbForSearchFieldList.newBuilder();
				List<SearchField> fields = XmlUtil.getAppSearchFields(appId);
				if (fields != null ){
					for (SearchField searchField : fields) {
						PbForSearchField.Builder pbForSearchField = PbForSearchField.newBuilder();
						PbSearchFieldBasic.Builder pbSearchFieldBasic = PbSearchFieldBasic.newBuilder();
						pbSearchFieldBasic.setIntAttrId(searchField.getIntAttrId());//属性ID
						pbSearchFieldBasic.setIntAttrType(searchField.getIntInputType());//属性输入类型
						pbForSearchField.setPbSearchFieldBasic(pbSearchFieldBasic);
						pbForSearchField.setVarAttrName(searchField.getVarAttrName());//属性名
						if (searchField.getIntInputType() == 1) {
							//选择型,查询选项
							List<AttributeListTab> lstArea = attributeListTabService.getOptionsByType(searchField.getIntType());
							if (lstArea != null) {
								PbSelectOptionList.Builder pbSelectOptionList = PbSelectOptionList.newBuilder();
								for (AttributeListTab attributeListTab : lstArea) {
									PbSelectOption.Builder pbSelectOption = PbSelectOption.newBuilder();
									pbSelectOption.setOptionKey(attributeListTab.getIntAttrListId());
									pbSelectOption.setOptionValue(attributeListTab.getVarAttrListValue());
									pbSelectOptionList.addPbSelectOption(pbSelectOption);
								}
								pbForSearchField.setPbSelectOptionList(pbSelectOptionList);
							}
						}
						pbForSearchFieldList.addPbForSearchField(pbForSearchField);
					}
					pBContentSearchFieldSC.setPbForSearchFieldList(pbForSearchFieldList);
					pBContentSearchFieldSC.setExeResult(PBRESULT.SUCCESS);
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_SUCCESS);
				} else {
					log.info(LOG_METHOD_NAME+Const.LOG_DATA_NULL);
					pBContentSearchFieldSC.setExeResult(PBRESULT.NULL);
				}
			} catch (Exception e) {//设置数据获取异常状态
				log.error(LOG_METHOD_NAME+Const.LOG_DATA_EXCEPTION,e);
				e.printStackTrace();
				pBContentSearchFieldSC.setExeResult(PBRESULT.EXCEPTION);
			}
		}else {//设置数据获取其他情况导致无法获得数据
			log.info(LOG_METHOD_NAME+Const.LOG_BYTE_NULL);
			pBContentSearchFieldSC.setExeResult(PBRESULT.FAILED);
		}
		return pBContentSearchFieldSC.build().toByteArray();
	}
	
	//Setter
	public void setContentTabService(ContentTabService contentTabService) {
		this.contentTabService = contentTabService;
	}

	public void setAttributeTabService(AttributeTabService attributeTabService) {
		this.attributeTabService = attributeTabService;
	}

	public void setAttributeValueTabService(
			AttributeValueTabService attributeValueTabService) {
		this.attributeValueTabService = attributeValueTabService;
	}

	public void setContentImgTabService(ContentImgTabService contentImgTabService) {
		this.contentImgTabService = contentImgTabService;
	}

	public void setCatelogTabService(CatelogTabService catelogTabService) {
		this.catelogTabService = catelogTabService;
	}

	public void setEnterpShopTabService(EnterpShopTabService enterpShopTabService) {
		this.enterpShopTabService = enterpShopTabService;
	}

	public void setAppDetailPageTabService(
			AppDetailPageTabService appDetailPageTabService) {
		this.appDetailPageTabService = appDetailPageTabService;
	}

	public void setAttributeListTabService(
			AttributeListTabService attributeListTabService) {
		this.attributeListTabService = attributeListTabService;
	}

	public void setPageModuleConTabService(
			PageModuleConTabService pageModuleConTabService) {
		this.pageModuleConTabService = pageModuleConTabService;
	}
}
